// page-personalInformationImg/home.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    infoData: {},
    enterprise: { sfzqm: '法人身份证正面', sfzbm: '法人身份证背面', hukou: '法人户口簿', peiouhukou: '营业执照副本', jiehunzheng: '介绍信', rentou: '代办人照片', otherIdentityPositive: '代理人身份证正面', otherIdentityOpposite: '代理人身份证背面' },
    my: { sfzqm: '身份证正面', sfzbm: '身份证背面照', rentou: '申请人照片', hukou: '申请人户口本', jiehunzheng: '婚姻证明', peiouhukou: '配偶户口簿', otherIdentityPositive: '配偶身份证正面', otherIdentityOpposite: '配偶身份证背面' },
    url: app.globalData.requestURL +'upload/uploadImgTest',
    path: '',//文件本地路径
    name: '',//name标识符
    imagesType: '',//自定义上传的目录路径
    identityNum: '',//身份证号
    sfzqm: wx.getStorageSync('sfzqm'), //身份证正面/法人身份证正面
    sfzbm: wx.getStorageSync('sfzbm'), //身份证背面/法人身份证背面
    rentou: wx.getStorageSync('rentou'), //申请人照片/代办人照片
    hukou: wx.getStorageSync('hukou'), //申请人户口簿/法人户口簿
    peiouhukou: wx.getStorageSync('peiouhukou'), //配偶户口簿//营业执照副本
    jiehunzheng: wx.getStorageSync('jiehunzheng'), //婚姻证明/介绍信
    otherIdentityPositive: wx.getStorageSync('otherIdentityPositive'),//配偶身份证正面/代理人身份证正面 
    otherIdentityOpposite: wx.getStorageSync('otherIdentityOpposite'),//配偶身份证背面/代理人身份证背面 
    maritalStatus: wx.getStorageSync('maritalStatus'),//婚姻状况
    userType: wx.getStorageSync('userType'),//0个人，   1企业
    qylxr: false,
    marriage: false,
    hyzm: true
  },
  chooseImage: function (e) {
    console.log(e.target.id)
    var self = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'],
      success: function (res) {
        if (e.target.id == '0') {
          self.setData({
            name: "sfzqm",
            sfzqm: res.tempFilePaths,
            path: res.tempFilePaths,
            imagesType: 'user/' + self.data.identityNum + '/' + "zhenjian"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('sfzqm', imgResponseUrl)
            self.setData({
              sfzqm: wx.getStorageSync('sfzqm')
            })
          };
        } else if (e.target.id == '1') {
          self.setData({
            name: "sfzbm",
            sfzbm: res.tempFilePaths,
            path: res.tempFilePaths,
            imagesType: 'user/' + self.data.identityNum + '/' + "zhenjian"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('sfzbm', imgResponseUrl)
            self.setData({
              sfzbm: imgResponseUrl
            })
          };
        } else if (e.target.id == '2') {
          self.setData({
            name: "rentou",
            rentou: res.tempFilePaths,
            path: res.tempFilePaths,
            imagesType: 'user/' + self.data.identityNum + '/' + "zhenjian"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('rentou', imgResponseUrl)
            self.setData({
              rentou: imgResponseUrl
            })
          };
        } else if (e.target.id == '3') {
          self.setData({
            name: "hukou",
            hukou: res.tempFilePaths,
            path: res.tempFilePaths,
            imagesType: 'user/' + self.data.identityNum + '/' + "zhenjian"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('hukou', imgResponseUrl)
            self.setData({
              hukou: imgResponseUrl
            })
          };
        } else if (e.target.id == '4') {
          self.setData({
            name: "peiouhukou",
            peiouhukou: res.tempFilePaths,
            path: res.tempFilePaths,
            imagesType: 'user/' + self.data.identityNum + '/' + "zhenjian"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('peiouhukou', imgResponseUrl)
            self.setData({
              peiouhukou: imgResponseUrl
            })
          };
        } else if (e.target.id == '5') {
          self.setData({
            name: "jiehunzheng",
            jiehunzheng: res.tempFilePaths,
            path: res.tempFilePaths,
            imagesType: 'user/' + self.data.identityNum + '/' + "zhenjian"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('jiehunzheng', imgResponseUrl)
            self.setData({
              jiehunzheng: imgResponseUrl
            })
          };
        } else if (e.target.id == '6') {
          self.setData({
            name: "qtsfzqm",
            otherIdentityPositive: res.tempFilePaths,
            path: res.tempFilePaths,
            imagesType: 'user/' + self.data.identityNum + '/' + "zhenjian"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('otherIdentityPositive', imgResponseUrl)
            self.setData({
              otherIdentityPositive: imgResponseUrl
            })
          };
        } else if (e.target.id == '7') {
          self.setData({
            name: "qtsfzbm",
            otherIdentityOpposite: res.tempFilePaths,
            path: res.tempFilePaths,
            imagesType: 'user/' + self.data.identityNum + '/' + "zhenjian"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('otherIdentityOpposite', imgResponseUrl)
            self.setData({
              otherIdentityOpposite: imgResponseUrl
            })
          };
        }
      }
    });
  },
  submit: function (e) {
    var that = this;
    var identityNum = this.data.identityNum;
    var identityPositiveImg = that.data.sfzqm;
    if (identityPositiveImg == '' || identityPositiveImg == null) {
      wx.showToast({
        title: '照片未添加',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      return false;
    }
    var identityOppositeImg = that.data.sfzbm;
    if (identityOppositeImg == '' || identityOppositeImg == null) {
      wx.showToast({
        title: '照片未添加',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      return false;
    }
    var portrait = that.data.rentou;
    if (portrait == '' || portrait == null) {
      wx.showToast({
        title: '照片未添加',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      return false;
    }
    var householdRegisterImg = that.data.hukou;
    if (householdRegisterImg == '' || householdRegisterImg == null) {
      wx.showToast({
        title: '照片未添加',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      return false;
    }
    var householdSpouseImg = that.data.peiouhukou;
    if (that.data.marriage && (householdSpouseImg == '' || householdSpouseImg == null)) {
      wx.showToast({
        title: '照片未添加',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      return false;
    }
    var marriageCertificate = that.data.jiehunzheng;
    if (that.data.hyzm && (marriageCertificate == '' || marriageCertificate == null)) {
      wx.showToast({
        title: '照片未添加',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      return false;
    }
    var otherIdentityPositive = that.data.otherIdentityPositive;
    if (that.data.marriage && (otherIdentityPositive == '' || otherIdentityPositive == null)) {
      wx.showToast({
        title: '照片未添加',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      return false;
    }
    var otherIdentityOpposite = that.data.otherIdentityOpposite;
    if (that.data.marriage && (otherIdentityOpposite == '' || otherIdentityOpposite == null)) {
      wx.showToast({
        title: '照片未添加',
        icon: 'loading',
        duration: 2500,
        mask: true
      });
      return false;
    }
    wx.request({
      url: app.globalData.requestURL +'personalInformationImg/save',
      method: 'POST',
      data: {
        identityNum: identityNum,
        identityPositiveImg: identityPositiveImg,
        identityOppositeImg: identityOppositeImg,
        portrait: portrait,
        householdRegisterImg: householdRegisterImg,
        householdSpouseImg: householdSpouseImg,
        marriageCertificate: marriageCertificate,
        otherIdentityPositive: otherIdentityPositive,
        otherIdentityOpposite: otherIdentityOpposite
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == '0') {
          wx.setStorageSync('sfzqm', ''); //身份证正面/法人身份证正面
          wx.setStorageSync('sfzbm', ''); //身份证背面/法人身份证背面
          wx.setStorageSync('rentou', ''); //申请人照片/代办人照片
          wx.setStorageSync('hukou', ''); //申请人户口簿/法人户口簿
          wx.setStorageSync('peiouhukou', ''); //配偶户口簿//营业执照副本
          wx.setStorageSync('jiehunzheng', ''); //婚姻证明/介绍信
          wx.setStorageSync('otherIdentityPositive', ''); //配偶身份证正面/代理人身份证正面 
          wx.setStorageSync('otherIdentityOpposite', ''); //配偶身份证背面/代理人身份证背面 
          wx.setStorageSync('marryStatus', ''); //婚姻状况
          wx.setStorageSync('userType', ''); //0个人，   1企业
          wx.setStorageSync('userImgSubmit', true);
          wx.switchTab({
            url: '/page-wode/home/home'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      identityNum: wx.getStorageSync('identification'),
      userType: wx.getStorageSync('userType'),
      maritalStatus:wx.getStorageSync('marryStatus')
    });
    if (this.data.userType == '1') {
      this.setData({
        infoData: this.data.enterprise,
        marriage: true
      })
    }
    if (this.data.userType == '0') {
      this.setData({
        infoData: this.data.my
      })
      if (this.data.maritalStatus == '0') {
        this.setData({
          marriage: true
        })
      }
      if (this.data.maritalStatus == '1') {
        this.setData({
          hyzm: false,
          marriage: false
        })
      }
    }
    if (wx.getStorageSync('sfzqm') == '' || wx.getStorageSync('sfzqm') == null) {
      this.setData({
        sfzqm: app.globalData.operationImage[0], //身份证正面/法人身份证正面
      })
    }
    if (wx.getStorageSync('sfzbm') == '' || wx.getStorageSync('sfzbm') == null) {
      this.setData({
        sfzbm: app.globalData.operationImage[1], //身份证背面/法人身份证背面
      })
    }
    if (wx.getStorageSync('rentou') == '' || wx.getStorageSync('rentou') == null) {
      this.setData({
        rentou: app.globalData.operationImage[2], //申请人照片/代办人照片
      })
    }
    if (wx.getStorageSync('hukou') == '' || wx.getStorageSync('hukou') == null) {
      this.setData({
        hukou: app.globalData.operationImage[3], //申请人户口簿/法人户口簿
      })
    }
    if (wx.getStorageSync('peiouhukou') == '' || wx.getStorageSync('peiouhukou') == null) {
      this.setData({
        peiouhukou: app.globalData.operationImage[4], //配偶户口簿//营业执照副本
      })
    }
    if (wx.getStorageSync('jiehunzheng') == '' || wx.getStorageSync('jiehunzheng') == null) {
      this.setData({
        jiehunzheng: app.globalData.operationImage[5], //婚姻证明/介绍信
      })
    }
    if (wx.getStorageSync('otherIdentityPositive') == '' || wx.getStorageSync('otherIdentityPositive') == null) {
      this.setData({
        otherIdentityPositive: '',//配偶身份证正面/代理人身份证正面 
      })
    }
    if (wx.getStorageSync('otherIdentityOpposite') == '' || wx.getStorageSync('otherIdentityOpposite') == null) {
      this.setData({
        otherIdentityOpposite: '',//配偶身份证背面/代理人身份证背面 
      })
    }
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})